﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using Sustainalytics.ESGRatings.BusinessLogic.Computing;
using Sustainalytics.ESGRatings.Computing.Entities.ESG;

namespace Sustainalytics.ESGComputing.Service.Controllers
{
    using Sustainalytics.ESGRatings.BusinessLogic.Computing.Exceptions;

    [RoutePrefix("api/v1/dataprovisioning/esgsets")]
    public class EsgWeightsController : ApiController
    {
        private readonly ComputationEngine _engine;

        public EsgWeightsController(ComputationEngine engine)
        {
            _engine = engine;
        }

        [Route("{setId}/weights/")]
        public HttpResponseMessage Get(Guid setId)
        {
            var data = _engine.GetMatrices(setId);

            return Request.CreateResponse(HttpStatusCode.OK, data);
        }

        [Route("{setId}/weights/")]
        public HttpResponseMessage Post(Guid setId, WeightMatrix data)
        {
            _engine.AddMatrices(setId, new[] { data });

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [Route("{setId}/weights/{id}")]
        public HttpResponseMessage Delete(Guid setId, Guid id)
        {
            if (setId.Equals(Guid.Empty))
            {
                throw new UnknownSetException(setId);
            }

            if (id.Equals(Guid.Empty))
            {
                throw new UnknownMatrixException(id); 
            }

            _engine.DeleteMatrix(setId, id);
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}